package server.model.players.packets;

import java.io.File;
import java.lang.reflect.Method;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.HashMap;
import server.Server;
import server.model.players.Client;

/**
 * @author Austin
 *
 */
public class Command {
	
	public Command() {
		
	}
	
	public static HashMap<String, Command> commandsList = new HashMap<String, Command>();
	
	
	
	/**
	 * 
	 * @param client
	 */
	public Command(Client client) {
		this.client = client;
		client.setCommand(this);
	}
	

	
	
	public boolean hasSplitArgs() {
		return false;
	}
	
	
	/* Override this! */
	public void onCall(String command) {
		
	}
	
	/* Override this! */
	public String getCommandName() {
		return null;
	}
	
	public Client getClient() {
		return client;
	}
	
	
	public Client client;
	
	
	/**
	 * Override this if you need to
	 * @return the rights required to use this command
	 */
	public int playerRightsRequired() {
		return 0;
	}
	

	
	public boolean processCommand(String command) {
	System.out.println("Processing: " + command);
		String args[] = command.split(" ");
		
		Command c = commandsList.get(args[0]);
		if(c == null) {
			System.out.println("Could not process: " + command);
			return false;
		}
		//Make sure they can do the command
		if(client.playerRights < c.playerRightsRequired()) {
			System.out.println("not high enough rank");
			return false;
		}
		c.onCall(command);
		return true;
	}
	
	@SuppressWarnings({ "unchecked", "deprecation" })
	public static void loadCommands() {
		long currentTime = System.currentTimeMillis();
		System.out.println("Loading Commands");
		File[] dir = new File(Server.commandsDirectory).listFiles();
		
		URL[] scriptURLS = new URL[100];
		int z = 0;
		for(File f : dir) {
			if(f == null) continue;
			try {
				if(!f.getName().contains(".class")) continue;
				if(f.getName().contains("$")) continue;
				scriptURLS[z] = f.toURL();
				z++;
				//System.out.println("Found: " + f.getName());
			} catch (MalformedURLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
		
		System.out.println("Commands: " + z);
		ClassLoader loader = new URLClassLoader(scriptURLS);
		try {
			for(URL u : scriptURLS) {
				if(u == null) continue;
				String fileName = u.getFile();
				int beginIndex = fileName.indexOf("commands/") + 9;
				int endIndex = fileName.indexOf(".c");
				fileName = fileName.substring(beginIndex, endIndex);
				System.out.println("Attempting to load: " + fileName);
		Class<Command> c = (Class<Command>) loader.loadClass("server.commands."+fileName);
		if(c == null) {
			System.out.println("NULL");
			continue;
		}
		
		Object instance = c.newInstance();
	Method m = c.getMethod("getCommandName");
		String name = (String)m.invoke(instance);
		if(Command.commandsList.containsKey(name)) continue;
		System.out.println("Found new Command: " + name);
		Command.commandsList.put(name, c.newInstance());
	
			}
		} catch(Exception e) {
			e.printStackTrace();
		}
		long time = System.currentTimeMillis() - currentTime;
		System.out.println("Command Loading took: " + (time) + " ms.");
		System.out.println("Average time per command: " + (time / Command.commandsList.size()));
	}
	
	
	
	

}
